<template>
  <div class="article-meta">
    <nuxt-link :to="{
      name: 'profile',
      params: {
        username: article.author.username
      }
    }">
      <img :src="article.author.image" />
    </nuxt-link>
    <div class="info">
      <nuxt-link :to="{
        name: 'profile',
        params: {
          username: article.author.username
        }
      }" class="author">{{ article.author.username }}</nuxt-link>
      <span class="date">{{ article.createAt | date('MMM DD, YYYY') }}</span>
    </div>

    <template v-if="isSelf">
      <nuxt-link 
        class="btn btn-outline-secondary btn-sm"
        :to="{
          name: 'editor',
          params: { slug: article.slug }
        }">
        <i class="ion-edit"></i> Edit Article
      </nuxt-link>

      <button class="btn btn-outline-danger btn-sm" @click="deleteArticle">
        <i class="ion-trash-a"></i> Delete Article
      </button>
    </template>

    <template v-else>
      <button
        class="btn btn-sm btn-outline-secondary"
        :class="{
          active: article.author.following
        }">
        <i class="ion-plus-round"></i>
        &nbsp;
        Follow {{ article.author.username }} <span class="counter"></span>
      </button>
      &nbsp;
      <button
        class="btn btn-sm btn-outline-primary"
        :class="{
          active: article.favorited
        }">
        <i class="ion-heart"></i>
        &nbsp;
        Favorite Post <span class="counter">({{ article.favoritesCount }})</span>
      </button>
    </template>

  </div>
</template>

<script>
import { mapState } from 'vuex'
import { deleteArticle } from '@/api'

export default {
  name: 'ArticleMeta',
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  computed: {
    ...mapState(['user']),
    isSelf () {
      if (!this.user) return false
      return this.article.author.username === this.user.username
    }
  },
  methods: {
    async deleteArticle () {
      await deleteArticle(this.article.slug)
      this.$router.push({
        name: 'home'
      })
    }
  }
}
</script>
